//
//  EeNumberView.swift
//  Aiwei
//
//  Created by zhu xietong on 2018/1/22.
//  Copyright © 2018年 zhu xietong. All rights reserved.
//

import Foundation
import Eelay

extension TypesCountView:EeNumObserver{
    public func changed(numbers: [(num: Int, msgType: EeMsgType)], allCount: Int) {
        
        if let  _ = JoUser.loginUser()
        {
            let all:Int = allCount
            
            
            switch style {
            case .numText(fontSize: _):
                if all > 0{
                    self.numL.alpha = 1
                    self.numL.setTitle("\(all)", for: .normal)
                }
                else{
                    self.numL.alpha = 0
                }
            case .redPoint(size: _):
                if all > 0{
                    self.numL.alpha = 1
                }
                else{
                    self.numL.alpha = 0
                }
            }
            return
        }
        self.numL.alpha = 0
        
    }
    
}

public class TypesCountView:UIView{
    
    public enum Style {
        case numText(fontSize:CGFloat)
        case redPoint(size:CGSize)
    }
    
    
    public var observerTypes: [EeMsgType] = [EeMsgType]()
    
    
    public var style:Style = Style.redPoint(size: CGSize(width: 10, height: 10))
    
    public var numL = UIButton()
    public func addLayoutRules() {
        
        switch self.style {
        case .redPoint(size: let size):
            eelay = [
                [numL,[ee.T.L.B.R],"\(size.height)".+1000,Int(size.width).+1000]
            ]
            //            numL.ui.text(color: .clear).set(radius: size.height/2.0, width: 0, color: nil)
            //            numL.ui.font(size: s)
            numL.backgroundColor = .red
            self.backgroundColor = .clear
            
        case .numText(fontSize: let font_size):
            let newHeight = font_size + 6
            
            eelay = [
                [numL,[ee.T.L.B.R],"\(newHeight)".+1000,.>Int(newHeight).+1000],
            ]
            
            numL.ui.buttonColor(color: .red).font(size: font_size).padding(insets: [3,5,3,5]).set(radius: newHeight/2.0, width: nil, color: nil)
            //            numL.backgroundColor = .red
            self.backgroundColor = .clear
            self.isUserInteractionEnabled = false
            
            EeNum.observers.append(self)
            
            EeNum.noticeToObsevers()
            
            //            numL.ui.text(color: .clear).set(radius: height/2.0, width: 0, color: nil)
            //            numL.font = UIFont.systemFont(ofSize: font_size)
            //            _ = numL.ui.cl_fff.center
            
        }
    }
    
    
    
    
    
    public init(style:Style) {
        self.style = style
        super.init(frame: CGRect.zero)
        addLayoutRules()
    }
    
    
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        
    }
    
}

public class EeNumberView:UIView
{
    public var redView = UIView()
    public var msgType:EeMsgType = EeMsgType(rawValue: "")
    {
        willSet{
            NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: self.msgType.NoticeName), object: nil)
        }
        didSet{
            NotificationCenter.default.addObserver(self, selector: #selector(checkState), name: NSNotification.Name(rawValue: self.msgType.NoticeName), object: nil)
            self.checkState()
        }
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public init(size:CGSize)
    {
        super.init(frame: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        
        self.eelay = [
            [redView,[ee.T.L.B.R]],
        ]
        redView.backgroundColor = UIColor.red
        redView.alpha = 0.0
        self.backgroundColor = UIColor.clear
        
        //        NotificationCenter.default.addObserver(self, selector: #selector(SoNumberView.checkState), name: NSNotification.Name(rawValue: type.NoticeName), object: nil)
        
        self.addNotice()
    }
    
    
    public override var frame: CGRect{
        set{
            super.frame = newValue
            self.layer.cornerRadius = newValue.height/2.0
            self.clipsToBounds = true
        }
        get{
            return super.frame
        }
    }
    
    public func addNotice()
    {
        
    }
    
    @objc public func checkState()
    {
        redView.alpha = 0.0
        
        if (EeNum.num(self.msgType)) > 0
        {
            redView.alpha = 1.0
        }
    }
    
    deinit
    {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: self.msgType.NoticeName), object: nil)
    }
    
    public override var intrinsicContentSize: CGSize{
        get{
            return self.frame.size
        }
    }
}




public class EeMarkView:UIView
{
    public var obj_id:String?
    {
        didSet{
            self.checkState()
        }
    }
    public var redView = UIView()
    public var type:EeMsgType
    
    public init(type:EeMsgType,size:CGFloat = 10)
    {
        self.type = type
        super.init(frame: CGRect(x: 0, y: 0, width: size, height: size))
        
        self.eelay = [
            [redView,[ee.T.L.B.R]],
        ]
        redView.backgroundColor = UIColor.red
        redView.alpha = 0.0
        redView.bsui.set(radius: size/2.0, width: 0, color: nil)
        self.backgroundColor = UIColor.clear
        
        NotificationCenter.default.addObserver(self, selector: #selector(checkState), name: type.Notice, object: nil)
        
        
    }
    
    @objc public func checkState()
    {
        if let o_id = self.obj_id
        {
            redView.alpha = 0.0
            if !(EeNum.read_state(o_id, type: self.type))
            {
                redView.alpha = 1.0
            }
        }
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit
    {
        NotificationCenter.default.removeObserver(self, name:type.Notice, object: nil)
    }
}



